<template>
  <div class="content-detail">
    <el-card class="card-box">
      <div class="card-box--headerwrap">
        <el-page-header
          icon="el-icon-arrow-left"
          content="审批详情"
          @back="handleBack"
        ></el-page-header>

        <div class="card-box--downloadbtn" v-if="source === 'self'">
          <el-button
            circle
            type="primary"
            @click="recall"
            v-if="nodeDefId != 'END'"
            >撤回</el-button
          >
          <el-button circle type="primary" @click="inform">知会</el-button>
        </div>

        <!-- <div class="card-box--downloadbtn" v-if="contractId">
          <el-tooltip effect="dark" content="预览" placement="bottom">
            <template #default>
              <el-button
                circle
                type="primary"
                icon="el-icon-view"
                @click="handleView"
              ></el-button>
            </template>
          </el-tooltip>
          <el-tooltip effect="dark" content="下载" placement="bottom">
            <template #default>
              <el-button
                type="primary"
                circle
                icon="el-icon-download"
                @click="handleDownload"
              ></el-button>
            </template>
          </el-tooltip>
        </div> -->
      </div>
    </el-card>
    <content-page
      :instanceId="instanceId"
      :contractId="contractId"
      :nodeDefId="nodeDefId"
      :isEdit="isEdit"
      :flowId="flowId"
      :flowType="flowType"
      :module="module"
      :billName="billName"
      :readId="readId"
      :approvalType="approvalType"
      :taskId="taskId"
      :source="source"
      :billNum="billNum"
      ref="contentRef"
    ></content-page>

    <el-dialog title="选择人员" v-model="dialogVisible" width="920px">
      <add-user
        v-model:dialogVisible="dialogVisible"
        :currentUsers="acceptUsers"
        :isSingle="true"
        @emitAddUser="submitInform"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
// import { downloadpdf, viewpdf } from "/@/utils/download";
import ContentPage from "./content.vue";
import AddUser from "../../projectsA/acceptance/concealed/components/dialog/AddUser.vue";
import NotifMessage from "/@/utils/message/notification";
import { flowNotify } from "/@/api/work";
import { storageLocal } from "/@/utils/storage";
import { ElMessage, ElMessageBox } from "element-plus";
import { postRecall } from "/@/api/acceptance";
const contentRef = ref<HTMLFormElement | null>(null) as any;
const router = useRouter();
const route = useRoute();
console.log(11111111);
console.log(route.query);
let nodeDefId = ref(route.query.nodeDefId || "ACT_010");
let isEdit = ref(route.query?.mode !== "read");
let flowId = ref(route.query.flowId);
let flowType = ref(route.query.flowType);
// let cacheBillName = ref(""); // 下载pdf使用的名称
let source = ref(route.query?.source);
let back = ref(route.query?.back);
let instanceId = ref(route.query?.instanceId);
let contractId = ref(route.query?.contractId);
let dialogVisible = ref(false);
let userInfo = storageLocal.getItem("info");
let module = ref(route.query?.module);
let billName = ref(route.query?.billName);
let readId = ref(route.query?.readId);
let approvalType = ref(route.query?.approvalType);
let billNum = ref(route.query?.billNum);
let taskId = ref(route.query?.taskId);
// 返回按钮事件
const handleBack = () => {
  let page = back.value || "todo";
  router.push(`/approval/${page}`);
};

//撤回按钮
const recall = () => {
  ElMessageBox.confirm("是否确认要撤回？", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      handleRecall();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "撤回取消"
      });
    });
};

//流程撤回
const handleRecall = async () => {
  try {
    const preNodeId = route.query.preNodeId ? route.query.preNodeId : "ACT_010";
    const params = { instanceId: instanceId.value, nodeId: preNodeId };
    const { code, message } = await postRecall(params);
    if (code === 200) {
      NotifMessage.success(message);
      handleBack();
    } else {
      message && NotifMessage.error(message);
    }
  } catch (err) {
    console.log(err);
  }
};

const inform = () => {
  if (instanceId.value) {
    dialogVisible.value = true;
  } else {
    NotifMessage.info("当前流程还未发起，暂无法知会");
    return;
  }
};

const submitInform = async parmas => {
  const notifys = [];
  const id = parmas?.idList[0].id;
  notifys.push(id);
  let params = {
    assignee: userInfo?.userId,
    instanceId: instanceId.value,
    taskId: contentRef.value.taskIdNotify,
    notifys: notifys
  };
  try {
    const { code, message } = await flowNotify(params);
    if (code === 200) {
      NotifMessage.success(message);
      dialogVisible.value = false;
      handleBack();
    } else {
      NotifMessage.error(message);
    }
  } catch (err) {
    console.log(err);
  }
};
// 下载事件
// const handleDownload = async () => {
//   if (!route.query?.contractId) {
//     return;
//   }
//   const { contractId, flowId, instanceId } = route.query;
//   const params = {
//     contractInfoId: contractId,
//     flowId: flowId,
//     instanceId: instanceId,
//     pdfName: cacheBillName.value
//   };
//   downloadpdf(params, "quality");
// };

// // 预览事件
// const handleView = async () => {
//   if (!route.query?.contractId) {
//     return;
//   }
//   const { contractId, flowId, instanceId } = route.query;
//   const params = {
//     contractInfoId: contractId,
//     flowId: flowId,
//     instanceId: instanceId
//   };
//   viewpdf(params, "quality");
// };
</script>

<style lang="scss" scoped></style>
